package com.zhentao.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.zhentao.domain.SQuestion;
import com.zhentao.mapper.SQuestionMapper;
import com.zhentao.result.Result;
import com.zhentao.service.FeiginService;
import com.zhentao.service.SQuestionService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class FeginServiceImpl {
    @Resource
   private FeiginService feiginService;
    @Resource
    SQuestionService sQuestionService;

    @Resource
    private SQuestionMapper sQuestionMapper;

    public Result addExaminationInformation(Integer subId){
        Set<SQuestion> sQuestions = new HashSet<>();
        Set<SQuestion> one = sQuestionMapper.listByTypeAndSubId(subId,1);
        Set<SQuestion> two = sQuestionMapper.listByTypeAndSubId(subId,2);
        Set<SQuestion> three = sQuestionMapper.listByTypeAndSubId(subId,3);
        Set<SQuestion> four = sQuestionMapper.listByTypeAndSubId(subId,4);
            System.out.println("-------");
            System.out.println(one.size());
            System.out.println(two.size());
            System.out.println(three.size());
            System.out.println(four.size());
            System.out.println("-------");
        if (one.size()<20) {
            return Result.ERROR("单选题题数不足");
        }else {
            while (sQuestions.size()<20){
                int i = RandomUtil.randomInt(0, one.size());
                sQuestions.add(one.toArray(new SQuestion[0])[i]);
            }
        }
        if (two.size()<10) {
            return Result.ERROR("多选题题数不足");
        }else {
            while (sQuestions.size()<30){
                int i = RandomUtil.randomInt(0, two.size());
                sQuestions.add(two.toArray(new SQuestion[0])[i]);
            }
        }
        if (three.size()<5) {
            return Result.ERROR("判断题题数不足");
        }else {
            while (sQuestions.size()<35){
                int i = RandomUtil.randomInt(0, three.size());
                sQuestions.add(three.toArray(new SQuestion[0])[i]);
            }
        }
        if (four.size()<5) {
            return Result.ERROR("简答题题数不足");
        }else {
            while (sQuestions.size()<40){
                int i = RandomUtil.randomInt(0, four.size());
//                sQuestions.add(four.get(i));
                sQuestions.add(four.toArray(new SQuestion[0])[i]);
            }
        }

        ArrayList<SQuestion> list = new ArrayList<>();

        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==1){
                list.add(sQuestion);
            }
        }
        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==2){
                list.add(sQuestion);
            }
        }
        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==3){
                list.add(sQuestion);
            }
        }
        for (SQuestion sQuestion : sQuestions) {
            if (sQuestion.getTypeId()==4){
                list.add(sQuestion);
            }
        }
        Collections.shuffle(list);
        Collections.sort(list, new Comparator<SQuestion>() {
            @Override
            public int compare(SQuestion q1, SQuestion q2) {
                // 假设 typeid 是整数类型
                return Integer.compare(q1.getTypeId(), q2.getTypeId());
            }
        });
        for (SQuestion sQuestion : list) {
            System.out.println("+++++++++"+sQuestion);
        }
        return Result.OK(list);
    }
}
